﻿
Partial Class Site_Login
    Inherits MyUI.PageBaseApp

    Const LoginPage = "~/Site/Login.aspx"
    Const MainPage = "~/"


    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        If UI.Page.GetValue("r") <> "" Then
            UI.Page.RedirectTopJS(Me.ResolveClientUrl(LoginPage))
        End If


        If Not Me.Page.IsPostBack Then
            SetDataPage()
        End If

    End Sub

    Private Sub SetDataPage()

        Dim au = New MyUI.AuthUsers()

        If (au.IsAuthenticated) Then
            Response.Redirect(MainPage)
        Else
            PnlNotLogged.Visible = True
            PnlLogged.Visible = False
        End If

    End Sub
    Protected Sub BtnLogin_Click(sender As Object, e As System.EventArgs) Handles BtnLogin.Click
        If Me.Page.IsValid Then
            Dim au = New MyUI.AuthUsers()

            au.LogIn(Me.TxtEmail.Text, Me.TxtPass.Text, Me.ChkRemeberme.Checked)
            If Not (au.IsAuthenticated) Then
                LblErrorLogin.Text = "Errore inserimento dati"
                LblErrorLogin.Visible = True

            End If

            SetDataPage()
        End If

    End Sub


    Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click
        Dim au = New MyUI.AuthUsers()
        au.LogOut()
        Response.Redirect("~/")

    End Sub

    Protected Sub BtnSendPassword_Click(sender As Object, e As EventArgs) Handles BtnSendPassword.Click

        Dim user = DB.TAB.User.Where("email", "=", Me.TxtEmailSend.Text).ToObj

        If user Is Nothing Then
            LblErrorSendPwd.Text = "Email non trovata"
            LblErrorSendPwd.Visible = True
        Else
            '------------------------------------------------
            ' invia l'email
            MyApp.Email.System.Queued(MyApp.Email.System.Type.USER_FORGOTTEN_PWD, True, New With {.user_id = user.Id})
            BtnSendPassword.Text = "Sent!"
        End If

        Me.AddJScript("$('#box_forgot_password').show();")

        SetDataPage()
    End Sub
End Class
